package com.yk.course.service.impl;

import java.util.List;

import com.yk.common.utils.DateUtils;
import com.yk.common.utils.ShiroUtils;
import com.yk.course.domain.CoursePeriod;
import com.yk.course.mapper.CoursePeriodMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yk.course.mapper.CourseInfoMapper;
import com.yk.course.domain.CourseInfo;
import com.yk.course.service.ICourseInfoService;
import com.yk.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程设置Service业务层处理
 *
 * @author xufei
 * @date 2025-04-28
 */
@Service
public class CourseInfoServiceImpl implements ICourseInfoService {
    @Autowired
    private CourseInfoMapper courseInfoMapper;
    @Autowired
    private CoursePeriodMapper coursePeriodMapper;

    /**
     * 查询课程设置
     *
     * @param id 课程设置主键
     * @return 课程设置
     */
    @Override
    public CourseInfo selectCourseInfoById(Long id) {
        return courseInfoMapper.selectCourseInfoById(id);
    }

    /**
     * 查询课程设置列表
     *
     * @param courseInfo 课程设置
     * @return 课程设置
     */
    @Override
    public List<CourseInfo> selectCourseInfoList(CourseInfo courseInfo) {
        return courseInfoMapper.selectCourseInfoList(courseInfo);
    }

    /**
     * 新增课程设置
     *
     * @param courseInfo 课程设置
     * @return 结果
     */
    @Override
    @Transactional
    public int insertCourseInfo(CourseInfo courseInfo) {
        courseInfo.setCreateBy(ShiroUtils.getLoginName());
        courseInfo.setCourseStatus("1");
        courseInfo.setCreateTime(DateUtils.getNowDate());
        courseInfo.setDelFlag("0");
        int a = courseInfoMapper.insertCourseInfo(courseInfo);
        System.err.println(courseInfo.getId());
        CoursePeriod coursePeriod = new CoursePeriod();
        coursePeriod.setInfoId(courseInfo.getId());
        coursePeriod.setCreateBy(ShiroUtils.getLoginName());
        coursePeriod.setCreateTime(DateUtils.getNowDate());
        coursePeriod.setDelFlag("0");
        coursePeriod.setScratchOut("0");
        for (int i = 0; i < courseInfo.getNum(); i++) {
            coursePeriod.setCourseNum(i + 1);
            coursePeriodMapper.insertCoursePeriod(coursePeriod);
        }
        return a;
    }

    /**
     * 修改课程设置
     *
     * @param courseInfo 课程设置
     * @return 结果
     */
    @Override
    public int updateCourseInfo(CourseInfo courseInfo) {
        courseInfo.setUpdateTime(DateUtils.getNowDate());
        courseInfo.setUpdateBy(ShiroUtils.getLoginName());
        return courseInfoMapper.updateCourseInfo(courseInfo);
    }

    /**
     * 批量删除课程设置
     *
     * @param ids 需要删除的课程设置主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteCourseInfoByIds(String ids) {
        coursePeriodMapper.deleteCoursePeriodByInfoIds(Convert.toStrArray(ids));
        return courseInfoMapper.deleteCourseInfoByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除课程设置信息
     *
     * @param id 课程设置主键
     * @return 结果
     */
    @Override
    public int deleteCourseInfoById(Long id) {
        return courseInfoMapper.deleteCourseInfoById(id);
    }
}
